package com.example.t10v32;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Solution {
    List<Student> studentsToExclude = new ArrayList<>();

    public List<Student> solve(List<Student> students,double cutoffGrade,int minStudentsPerCourse) {
        List<Student> res = new ArrayList<>();
        for (int course = 1; course <= 2; course++) {
            List<Student> studentsOnCourse = filterStudentsByCourse(students, course);
            studentsOnCourse.sort(Comparator.comparingDouble(Student::getAverageGrade));
            int numStudentsToExclude = studentsOnCourse.size() - minStudentsPerCourse;
            if (numStudentsToExclude > 0) {
                double cutoffGradeForCourse = studentsOnCourse.get(numStudentsToExclude - 1).getAverageGrade();
                if (cutoffGradeForCourse < cutoffGrade) {
                    for (int i = 0; i < numStudentsToExclude; i++) {
                        studentsToExclude.add(studentsOnCourse.get(i));
                    }
                }
            }
        }
        if(studentsToExclude.size() > 0){
            for(var i:students){
                if(!studentsToExclude.contains(i)){
                    res.add(i);
                }
            }
        }
        return res;
    }
    private static List<Student> filterStudentsByCourse(List<Student> students, int course) {
        List<Student> filteredStudents = new ArrayList<>();
        for (Student student : students) {
            if (student.getCourse() == course) {
                filteredStudents.add(student);
            }
        }
        return filteredStudents;
    }
}
